<template>
  <el-dialog :close-on-click-modal="false" :visible.sync="dialog" title="流程配置" append-to-body width="800px">
    <el-row>
      <el-col>
        <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="80px">
          <el-form-item label="流程Key" prop="busMainData.flowKey">
            <el-select v-model="form.busMainData.flowKey" style="width: 370px;" placeholder="请选择">
              <el-option
                v-for="(item, index) in workflowDefList"
                :key="item.flowKey + index"
                :label="item.flowKey + '(' + item.flowName + ')'"
                :value="item.flowKey"/>
            </el-select>
          </el-form-item>
          <el-form-item label="流程名称" prop="busMainData.flowName">
            <el-input v-model="form.busMainData.flowName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.busMainData.rmk" style="width: 370px;" rows="2" type="textarea"/>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row>
      <el-col style="height:700px">>
        <el-card class="box-card" shadow="never">
          <div slot="header" class="clearfix">
            <div id="opt" style="float: left">
              <el-radio-group v-model="opt" size="mini">
                <el-radio-button label="menuRadio">模块分配</el-radio-button>
                <el-radio-button label="formRadio">表单分配</el-radio-button>
                <el-radio-button label="orgsetRadio">组织分配</el-radio-button>
              </el-radio-group>
            </div>
          </div>
          <el-tree
            v-show="opt === 'menuRadio'"
            ref="menu"
            :data="menus"
            :default-checked-keys="menuIds"
            :props="defaultProps"
            accordion
            default-expand-all
            show-checkbox
            node-key="id"/>
          <workflowFormList v-show="opt === 'formRadio'" ref="workflowFormList" :main-id="form.busMainData.id || form.busMainData.oldMainId" :editable="form.busPageInfo.editable" :sup_this="sup_this" />
          <el-tree
            v-show="opt === 'orgsetRadio'"
            ref="org"
            :data="orgs"
            :default-checked-keys="orgIds"
            :props="defaultProps"
            :check-strictly="true"
            default-expand-all
            show-checkbox
            accordion
            node-key="id"/>
        </el-card>
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <module-extend :editable="form.busPageInfo.editable" :source-form-object="formObject" :form-apply-id="form.busExtend.formApplyId" module-bus-class="100011-001"/>
      <el-button v-if="form.busPageInfo.editable" type="text" @click="cancel">取消</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','WORKFLOW_CONFIG_ALL','WORKFLOW_CONFIG_CHANGE','WORKFLOW_CONFIG_CREATE']" v-if="form.busPageInfo.editable" :loading="loading" type="primary" @click="doSave">暂存</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','WORKFLOW_CONFIG_ALL','WORKFLOW_CONFIG_SUBMIT']" v-if="form.busPageInfo.editable" :loading="loading" type="primary" @click="doSubmit">提交</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addOrEdit, viewMainData, toAdd, toChange, toCopy, getConfigOrgList, getConfigModuleList } from '@/api/system/workflowConfig'
import { getAllFlowDef } from '@/api/system/workflowDef'
import workflowFormList from './workflowFormList'
import { getAllWorkflowMenusTree } from '@/api/system/menu'
import { getOrgSetsByCpyCode } from '@/api/system/orgSet'
import moduleExtend from '@/views/components/moduleExtend'

export default {
  components: { workflowFormList, moduleExtend },
  props: {
    sup_this: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      formObject: this,
      isAdd: true,
      loading: false,
      formLoading: false,
      dialog: false,
      opt: 'menuRadio',
      menus: [], menuIds: [],
      orgs: [], orgIds: [],
      workflowDefList: [],
      form: {
        formSubmit: false,
        configModuleVOList: [],
        configOrgsetVOList: [],
        configFormVOList: [],
        busPageInfo: {
          editable: true
        },
        busMainData: {
          id: null,
          flowName: '',
          flowKey: '',
          enableFlag: 'Y',
          flowType: '1',
          selfState: '0',
          oldMainId: ''
        },
        busCommonState: {
        },
        busExtend: {
        }
      },
      rules: {
        'busMainData.flowKey': [
          { required: true, message: '请选择流程Key', trigger: 'blur' }
        ],
        'busMainData.flowName': [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 2, max: 100, message: '长度在 2 到 100 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initDataOfBefore() {
      this.orgIds = []
      this.menuIds = []
      this.getAllFlowDef()
      this.getAllOrgs()
      this.getAllWorkflowMenusTree()
    },
    getAllWorkflowMenusTree() {
      getAllWorkflowMenusTree().then(res => {
        this.menus = res.data
      })
    },
    getAllOrgs() {
      getOrgSetsByCpyCode().then(res => {
        this.orgs = res.data
      })
    },
    getAllFlowDef() {
      getAllFlowDef().then(res => {
        this.workflowDefList = res.data
      })
    },
    viewMainData(mainId) {
      const data = { paramBean: { id: mainId }}
      viewMainData(data).then(res => {
        this.form = res.data
      })
      this.getOtherDetailData(mainId)
    },
    addData() {
      const data = { paramBean: { }}
      toAdd(data).then(res => {
        this.dialog = true
        this.form = res.data
      }).catch(err => {
        this.dialog = false
        console.log(err)
      })
    },
    copyData(mainId) {
      const data = { paramBean: { id: mainId }}
      toCopy(data).then(res => {
        this.dialog = true
        this.form = res.data
        this.getOtherDetailData(mainId)
      }).catch(err => {
        this.dialog = false
        console.log(err)
      })
    },
    changeData(busId) {
      const data = { paramBean: { id: busId }}
      toChange(data).then(res => {
        this.dialog = true
        this.form = res.data
        this.getOtherDetailData(this.form.busMainData.oldMainId)
      }).catch(err => {
        this.dialog = false
        console.log(err)
      })
    },
    getOtherDetailData(mainId) {
      // 处理模块数据
      getConfigModuleList(mainId).then(res => {
        const menus = res.data
        const tmpMenuIds = []
        menus.forEach(function(row, index) {
          tmpMenuIds.push(row.menuId)
        })
        this.menuIds = tmpMenuIds
      })
      // 处理组织数据
      getConfigOrgList(mainId).then(res => {
        const orgs = res.data
        const tmpOrgIds = []
        orgs.forEach(function(row, index) {
          tmpOrgIds.push(row.sysOrgId)
        })
        this.orgIds = tmpOrgIds
      })
    },
    doSubmit() {
      this.doSingleSave(true)
    },
    doSave() {
      this.doSingleSave(false)
    },
    doSingleSave(isSubmit) {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.formSubmit = isSubmit
          // 组装组织
          const configOrgsetVOList = []
          this.$refs.org.getCheckedNodes().forEach(function(tmpNode, index) {
            const tmpObj = { sysOrgId: tmpNode.id, sysOrgCode: tmpNode.orgCode }
            configOrgsetVOList.push(tmpObj)
          })
          this.form.configOrgsetVOList = configOrgsetVOList

          // 组装菜单
          const configModuleVOList = []
          this.$refs.menu.getCheckedNodes().forEach(function(tmpNode, index) {
            const tmpObj = { menuId: tmpNode.id, busClass: tmpNode.moduleBusClass }
            configModuleVOList.push(tmpObj)
          })
          this.form.configModuleVOList = configModuleVOList

          // 组装表单数组
          const configFormVOList = []
          this.$refs.workflowFormList.configFormList.forEach(tmpvo => {
            const tmpObj = { formCode: tmpvo.sysGenCode }
            configFormVOList.push(tmpObj)
          })
          this.form.configFormVOList = configFormVOList

          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else {
            this.doEdit()
          }
        }
      })
    },
    doAdd() {
      addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  ::v-deep .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
